﻿using System.Collections.Generic;
using System.Timers;
using System.Windows.Forms;
using Timer = System.Timers.Timer;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;

namespace LoLTimers
{
    public sealed class TimerManager
    {
        private List<TimerDisplay> _timerDisplays = new List<TimerDisplay>();
        private Timer _updateTimer = new Timer();
        private KeyboardHookListener _keyboardHookListener;

        public List<TimerDisplay> TimerDisplays
        {
            get { return _timerDisplays; }
        }

        public void StartTimers()
        {
            if (_timerDisplays.Count > 0)
            {
                _keyboardHookListener = new KeyboardHookListener(new GlobalHooker());
                _keyboardHookListener.Enabled = true;
                _keyboardHookListener.KeyDown += new System.Windows.Forms.KeyEventHandler(_keyboardHookListener_KeyDown);
                _keyboardHookListener.KeyUp += new System.Windows.Forms.KeyEventHandler(_keyboardHookListener_KeyUp);
                ShowTimers();
            }
        }

        void _keyboardHookListener_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {

            foreach (TimerDisplay timerDisplay in _timerDisplays)
            {
                if ((e.KeyCode == Keys.LShiftKey || e.KeyCode == Keys.LControlKey || ((Control.ModifierKeys & Keys.Alt) != 0)) && timerDisplay.Bounds.Contains(Control.MousePosition))
                {
                    timerDisplay.SetActive(true);
                }
                else
                {
                    timerDisplay.SetActive(false);
                }

                if (timerDisplay.TimerData.ShortcutKey == e.KeyData)
                {
                    timerDisplay.ToggleTimer();
                }
            }
        }

        void _keyboardHookListener_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            foreach (TimerDisplay timerDisplay in _timerDisplays)
            {
                timerDisplay.SetActive(false);
            }
        }

        public void HideTimers()
        {
            _updateTimer.Stop();
            _updateTimer.Enabled = false;
            foreach (TimerDisplay timerDisplay in _timerDisplays)
            {
                timerDisplay.Hide();
            }
        }

        public void ShowTimers()
        {
            foreach (TimerDisplay timerDisplay in _timerDisplays)
            {
                timerDisplay.Show();
            }
            _updateTimer.Start();
            _updateTimer.Enabled = true;
        }

        public void SetAllTimersActive(bool isActive)
        {
            foreach (TimerDisplay timerDisplay in _timerDisplays)
            {
                timerDisplay.SetActive(isActive);
            }
        }

        public void StopTimers()
        {
            _updateTimer.Stop();
            _updateTimer.Enabled = false;
        }

        public void RegisterTimer(TimerDisplay timerDisplay)
        {
            if (!_timerDisplays.Contains(timerDisplay))
            {
                _timerDisplays.Add(timerDisplay);
            }
        }

        public void UnregisterTimer(TimerDisplay timerDisplay)
        {
            _timerDisplays.Remove(timerDisplay);
        }

        public void ClearTimers()
        {
            _timerDisplays.Clear();
        }

        public void SetUpdateTimerInterval(int miliseconds)
        {
            _updateTimer.Interval = miliseconds;
        }

        public int GetUpdateTimerInterval()
        {
            return (int)_updateTimer.Interval;
        }

        public void KillKeyboardHook()
        {
            if (_keyboardHookListener != null)
                _keyboardHookListener.Dispose();
        }

        private void UpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            foreach (TimerDisplay timerDisplay in _timerDisplays)
            {
                timerDisplay.UpdateTimer();
            }
        }

        #region Singleton Crap
        private static volatile TimerManager _instance;
        private static object _syncRoot = new object();

        private TimerManager()
        {
            _updateTimer.Stop();
            _updateTimer.Elapsed += new ElapsedEventHandler(UpdateTimer_Elapsed);
        }

        public static TimerManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncRoot)
                    {
                        if (_instance == null)
                            _instance = new TimerManager();
                    }
                }
                return _instance;
            }
        }
        #endregion

    }
}
